var express = require('express')
var path = require('path')
var cookieParser = require('cookie-parser')
var logger = require('morgan')
var cors = require('cors')
const Authenticatepolicy = require('./policies/Authenticatepolicy')

var indexRouter = require('./routes/index')
var usersRouter = require('./routes/users')
var todosRouter = require('./routes/todos')
var cateRouter = require('./routes/cate')
var loginRouter = require('./routes/login')
var uploadRouter = require('./routes/postadd')
var categoryRouter = require('./routes/category')
var postRouter = require('./routes/posts')
var userChange = require('./routes/userChange')
var revisionRouter = require('./routes/revision')
var enrollRouter = require('./routes/enroll')
var frontHomeRouter = require('./routes/front/home')
var frontListRouter = require('./routes/front/list')
var frontMessageRouter = require('./routes/front/message')
var frontEchartRouter = require('./routes/front/echart')

var app = express()

app.use(cors())
app.use(logger('dev'))
app.use(express.json())
app.use(express.urlencoded({ extended: false }))
app.use(cookieParser())
app.use(express.static(path.join(__dirname, 'public')))

app.use('/upload', express.static('uploads/images'))
app.use('/login', loginRouter)
app.use('/', indexRouter)
app.use('/userchange', Authenticatepolicy.isValidToken, userChange)
app.use('/revision', revisionRouter)
app.use('/enroll', enrollRouter)
app.use('/users', Authenticatepolicy.isValidToken, usersRouter)
app.use('/todos', Authenticatepolicy.isValidToken, todosRouter)
app.use('/posts', Authenticatepolicy.isValidToken, postRouter)
app.use('/cate', Authenticatepolicy.isValidToken, cateRouter)
app.use('/category', Authenticatepolicy.isValidToken, categoryRouter)
app.use('/postadd', uploadRouter)

app.use('/front/home', frontHomeRouter)
app.use('/front/list', frontListRouter)
app.use('/front/message', frontMessageRouter)
app.use('/front/echart', frontEchartRouter)

module.exports = app
